package com.jysz.certificate.persistence.dao.system;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.jysz.certificate.persistence.dao.AbstractBaseDao;
import com.jysz.certificate.persistence.enums.OrganizationEnum;
import com.jysz.certificate.persistence.enums.PostType;
import com.jysz.certificate.persistence.model.system.Organization;
import com.jysz.certificate.persistence.model.system.Region;
import com.jysz.certificate.persistence.model.system.User;

/**
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@Repository
public class RegionDao extends AbstractBaseDao<Region, Long> {

	public Integer getMaxOrder(Organization organization) {
		Criteria criteria = this.createCriteria();
		criteria.setProjection(Projections.max(Organization.COLUMN_ORDER));
		if(organization != null){
			criteria.add(Restrictions.eq(Organization.COLUMN_PARENT, organization));
		}else{
			criteria.add(Restrictions.eq(Organization.COLUMN_GRADE, 1));
		}
		Object result = criteria.uniqueResult();
		if(result == null || Integer.valueOf(result.toString()) == 0){
			return 0;
		}
		
		return Integer.valueOf(result.toString());
	}

	@SuppressWarnings("unchecked")
	public List<Organization> findTopOrganizations() {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq(Organization.COLUMN_GRADE, 1));
		criteria.addOrder(Order.asc(Organization.COLUMN_CODE));
		criteria.addOrder(Order.desc(Organization.COLUMN_ORDER));
		
		return criteria.list();
	}

	/**
	 * 或者该用户所管理的店铺。
	 * @param user
	 * @return
	 */
	public List<Organization> findShopsByCurrentUser(User user) {
		

		Criteria criteria = createCriteria();
//		List<Organization> shops = new ArrayList<Organization>();
		criteria.add(Restrictions.eq(Organization.COLUMN_TYPE, OrganizationEnum.SHOP));
		if(PostType.Admin.equals(user.getPost())){
			
		}else{
			List<Organization> orgs = findOrgByUser(user);
			
			if(orgs == null || orgs.size() < 1){
				return null;
			}
			Disjunction disjunction = Restrictions.disjunction();
			
			for (Iterator<Organization> iterator = orgs.iterator(); iterator.hasNext();) {
				Organization organization =  iterator.next();
				
				if(OrganizationEnum.SHOP.equals(organization.getParent().getType())){
					disjunction.add(Restrictions.eq(Organization.COLUMN_CODE, organization.getParent().getCode()));
//					shops.add(organization);
				}else{
					disjunction.add(Restrictions.like(Organization.COLUMN_CODE, organization.getParent().getCode(), MatchMode.START));
				}
			}
			
			criteria.add(disjunction);
		}
		
		return criteria.list();
	}

	/**
	 * 获取用户在组织架构上的位置节点
	 * 
	 * @param user
	 * @return
	 */
	public List<Organization> findOrgByUser(User user) {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq(Organization.COLUMN_TYPE, OrganizationEnum.STAFF));
		criteria.add(Restrictions.eq(Organization.COLUMN_OBJECT_ID, user.getId()));
//		List<Organization> orgs = criteria.list();
		return criteria.list();
	}

	/**
	 * @param shopId
	 * @return
	 */
	public List<Organization> findUserShopByShopId(Long shopId) {
		if(shopId == null ){
			return null;
		}
		Organization org = findShopById(shopId);
		
		if(org != null){
			return org.getChilds();
		}
		return null;
	}

	private Organization findShopById(Long shopId) {
		Criteria criteria = this.createCriteria();
		
		criteria.add(Restrictions.eq(Organization.COLUMN_TYPE, OrganizationEnum.SHOP));
		criteria.add(Restrictions.eq(Organization.COLUMN_OBJECT_ID, shopId));
		Object object = criteria.uniqueResult();
		
		return object != null ? (Organization)object : null;
	}

	/**
	 * 将店铺上的机构 认为是区域经理。
	 * 
	 * @param shopId
	 * @return
	 */
	public List<Organization> findUserDomainByShopId(Long shopId) {
		Organization shop = this.findShopById(shopId);
		if(shop == null ){
			return null;
		}
		Criteria criteria = this.createCriteria();
		System.out.println( shop.getParent());
		criteria.add(Restrictions.eq(Organization.COLUMN_PARENT, shop.getParent()));
		System.out.println(shop.getParent().getId());
		criteria.add(Restrictions.eq(Organization.COLUMN_TYPE, OrganizationEnum.STAFF));
		
		return criteria.list();
	}
	
	/**
	 * 根据区域经理的用户ID获得区域名称
	 * 
	 * @param userId
	 * @return
	 */
	public List<Organization> getAreaByUserManagerId(Long userId){
		List<Organization> areaList = new ArrayList<Organization>();
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq(Organization.COLUMN_OBJECT_ID, userId));
		criteria.add(Restrictions.eq(Organization.COLUMN_TYPE, OrganizationEnum.STAFF));
		List<Organization> managerList = criteria.list();
		for (Organization organization : managerList) {
			areaList.add(organization.getParent());
		}
		return areaList;
	}

}
